"""
Case Type   : GUC参数--资源消耗
Case Name   : 修改参数shared_buffers为无效值
Create At   : 2020/11/17
Owner       :opentestcase023
Description :
      1、查看shared_buffers默认值；
      show shared_buffers;
      2、修改shared_buffers分别为15、1073741824、1024.25、
      'test'校验其预期结果；
Expect      :
      1、显示默认值为1GB；
      2、参数修改失败；
History     :
     modified：2021-04-08 by zopentestcase005;修改用例,添加边界值校验
     modified：2022-05-18 by opentestcase040;修改用例,刪除shared_buffers參數默认值断言
"""

import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node

COMMONSH = CommonSH('PrimaryDbUser')


class GucTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.log.info('==Opengauss_Function_Guc_Resource_Case0024开始==')
        self.db_user_node = Node(node='PrimaryDbUser')

    def test_startdb(self):
        self.log.info("查询该参数默认值")
        sql_cmd = COMMONSH.execut_db_sql('show shared_buffers;')
        self.log.info(sql_cmd)
        self.res = sql_cmd.splitlines()[-2].strip()
        self.log.info("修改shared_buffers为无效值")
        invalid_value = ['15', '1073741824', '1024.25', 'test']
        for i in invalid_value:
            result = COMMONSH.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f"shared_buffers={i}")
            self.assertFalse(result)

    def tearDown(self):
        self.log.info("恢复默认值")
        sql_cmd = COMMONSH.execut_db_sql('show shared_buffers;')
        self.log.info(sql_cmd)
        msg = COMMONSH.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                     f'''shared_buffers={self.res}''')
        self.log.info(msg)
        msg = COMMONSH.restart_db_cluster()
        self.log.info(msg)
        status = COMMONSH.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        self.log.info('==Opengauss_Function_Guc_Resource_Case0024完成==')
